package com.wyl.clinic.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyl.clinic.utils.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 假设你的Token格式是 "Bearer token"
        String token = request.getHeader("Authorization");

        // 验证Token
        if (!JwtUtil.validateToken(token, request)) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(new ObjectMapper().writeValueAsString(Result.fail(401,"用户信息异常")));
        }

        return true;
    }

}